package fun.ticsmyc.question;

import org.junit.Test;

import java.util.Stack;

/**
 * @author Ticsmyc
 * @package fun.ticsmyc.dataStructure
 * @date 2020-02-17 23:05
 */
public class 二叉树的前驱后继节点 {
    public static class Node {
        public int value;
        public Node left;
        public Node right;
        public Node parent;

        public Node(int data) {
            this.value = data;
        }
    }

    /**
     * 寻找node节点的后继节点
     * @param node
     * @return
     */
    public static Node getSuccessorNode(Node node){
        //如果node有右子树，就找右子树的最左节点
        Node cur =node;
        if(cur.right!=null){
            cur=cur.right;
            while(cur.left!=null){
                cur=cur.left;
            }
            return cur;
        }else{
            //没有右子树，就看当前节点是不是父节点的左孩子
            Node parent =cur.parent;
            while(parent != null &&parent.left != cur){
                cur=parent;
                parent=parent.parent;
            }
            if(parent ==null){
                return null;
            }else{
                return parent;
            }
        }
    }

    /**
     * 中序遍历
     * @param root
     */
    public static void inOrderUnRecur(Node root){
        Stack<Node> stack = new Stack<>();
        while(root != null || !stack.empty()){
            if(root !=null){
                stack.push(root);
                root=root.left;
            }else{
                root=stack.pop();
                System.out.print(root.value+" ");
                root=root.right;
            }
        }
    }

    @Test
    public void test() {
        Node head = new Node(6);
        head.parent = null;
        head.left = new Node(3);
        head.left.parent = head;
        head.left.left = new Node(1);
        head.left.left.parent = head.left;
        head.left.left.right = new Node(2);
        head.left.left.right.parent = head.left.left;
        head.left.right = new Node(4);
        head.left.right.parent = head.left;
        head.left.right.right = new Node(5);
        head.left.right.right.parent = head.left.right;
        head.right = new Node(9);
        head.right.parent = head;
        head.right.left = new Node(8);
        head.right.left.parent = head.right;
        head.right.left.left = new Node(7);
        head.right.left.left.parent = head.right.left;
        head.right.right = new Node(10);
        head.right.right.parent = head.right;

        System.out.print("in-order: ");
        inOrderUnRecur(head);
        System.out.println();

        Node test = head.left.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.left.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.right.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.left.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.right; // 10's next is null
        System.out.println(test.value + " next: " + getSuccessorNode(test));
    }
}
